<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-header">
        <h1 class="login-title">Database Universe</h1>
        <p class="login-subtitle">数据库元数据管理系统</p>
      </div>

      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        size="large"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="用户名"
            prefix-icon="User"
            clearable
            @keyup.enter="handleLogin"
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="密码"
            prefix-icon="Lock"
            show-password
            clearable
            @keyup.enter="handleLogin"
          />
        </el-form-item>

        <el-form-item>
          <el-checkbox v-model="loginForm.rememberMe">
            记住登录状态
          </el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            class="login-button"
            :loading="loginLoading"
            @click="handleLogin"
          >
            {{ loginLoading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>
      </el-form>

      <div class="login-footer">
        <p class="footer-text">
          第一个用户将自动成为超级管理员
        </p>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed, onMounted, nextTick } from 'vue'
import { useRouter } from 'vue-router'
import { type FormInstance, type FormRules } from 'element-plus'
import { useAuthStore } from '@/stores/auth'
import type { LoginRequest } from '@/types/business/auth'

const router = useRouter()
const authStore = useAuthStore()

// 表单引用
const loginFormRef = ref<FormInstance>()

// 表单数据
const loginForm = reactive<LoginRequest>({
  username: '',
  password: '',
  rememberMe: false
})

// 表单验证规则
const loginRules: FormRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 50, message: '用户名长度在 3 到 50 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于 6 位', trigger: 'blur' }
  ]
}

// 计算属性
const loginLoading = computed(() => authStore.loginLoading)

// 登录处理
const handleLogin = async () => {
  if (!loginFormRef.value) return

  try {
    await loginFormRef.value.validate()

    const success = await authStore.login(loginForm)
    if (success) {
      // 等待一个tick确保状态更新完成
      await nextTick()

      // 登录成功，跳转到首页
      router.push('/')
    }
  } catch (error) {
    console.error('表单验证失败:', error)
  }
}

// 检查是否已登录
onMounted(() => {
  if (authStore.isAuthenticated) {
    router.push('/')
  }
})
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, var(--dbu-primary-500), var(--dbu-primary-700));
  padding: var(--dbu-space-4);
}

.login-card {
  background: var(--dbu-bg-primary);
  border-radius: var(--dbu-radius-xl);
  box-shadow: var(--dbu-shadow-xl);
  padding: var(--dbu-space-12);
  width: 100%;
  max-width: 400px;
}

.login-header {
  text-align: center;
  margin-bottom: var(--dbu-space-8);
}

.login-title {
  font-size: var(--dbu-text-3xl);
  font-weight: 700;
  color: var(--dbu-primary-600);
  margin-bottom: var(--dbu-space-2);
}

.login-subtitle {
  color: var(--dbu-gray-600);
  font-size: var(--dbu-text-base);
  margin: 0;
}

.login-form {
  margin-bottom: var(--dbu-space-6);
}

.login-button {
  width: 100%;
  height: 44px;
  font-size: var(--dbu-text-base);
  font-weight: 500;
  background: linear-gradient(135deg, var(--dbu-primary-500), var(--dbu-primary-600));
  border: none;
  border-radius: var(--dbu-radius-md);
  transition: all var(--dbu-transition-normal);
}

.login-button:hover {
  background: linear-gradient(135deg, var(--dbu-primary-600), var(--dbu-primary-700));
  transform: translateY(-1px);
  box-shadow: var(--dbu-shadow-md);
}

.login-footer {
  text-align: center;
  padding-top: var(--dbu-space-6);
  border-top: 1px solid var(--dbu-border-light);
}

.footer-text {
  color: var(--dbu-gray-500);
  font-size: var(--dbu-text-sm);
  margin: 0;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-card {
    padding: var(--dbu-space-8);
    margin: var(--dbu-space-4);
  }

  .login-title {
    font-size: var(--dbu-text-2xl);
  }
}

/* Element Plus 组件样式覆盖 */
:deep(.el-input__wrapper) {
  border-radius: var(--dbu-radius-md);
  box-shadow: var(--dbu-shadow-sm);
  transition: all var(--dbu-transition-normal);
}

:deep(.el-input__wrapper:hover) {
  box-shadow: var(--dbu-shadow-md);
}

:deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 2px var(--dbu-primary-200);
}

:deep(.el-checkbox__label) {
  color: var(--dbu-gray-700);
  font-weight: 400;
}

:deep(.el-form-item__error) {
  font-size: var(--dbu-text-xs);
  color: var(--dbu-error);
}
</style>